package com.zsp.utils.security_jwt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private PasswordUtil passwordUtil;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginDetails) {
        String username = loginDetails.get("username");
        String password = loginDetails.get("password");
        UserLogin user = new UserLogin();
        user.setUsername("zsp");
        user.setPassword("$2a$10$bPTFxrLWb53PGTJHAFyEsuj15C2HXlt3Q/f9n/Vcx28vXh0FEg1fu");
        if (username.equals(user.getUsername()) && passwordUtil.matches(password, user.getPassword())) {
            String token = jwtUtil.generateToken(user);
            Map<String, Object> response = new HashMap<>();
            response.put("token", token);
            return response;
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("msg", "账户/密码错误");
            response.put("code", 200);
            return response;
        }
    }
    @PostMapping("/getUserInfo")
    public void getUserInfo(){
        System.out.println(SecurityUtils.getUser());
    }
}
